package mesweb.mesweb.controller.sale;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import com.power.common.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.sale.SaleOrderMst;
import mesweb.mesweb.service.sale.ISaleOrderMstService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 订单
 * @Author: jeecg-boot
 * @Date:   2021-07-02
 * @Version: V1.0
 */
@Api(tags="订单")
@RestController
@RequestMapping("/mes/saleOrderMst")
public class SaleOrderMstController {
	@Autowired
	private ISaleOrderMstService saleOrderMstService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<SaleOrderMst> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = saleOrderMstService.list();
        } else {
            QueryWrapper<SaleOrderMst> queryWrapper = new QueryWrapper<>();
            data = saleOrderMstService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

	/**
	 *
	 * @param orderNo
	 * @param productNo
	 * @param procedureNo
	 * @param updateStartTime
	 * @param updateEndTime
	 * @param scheduleDate
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="订单-分页列表查询", notes="订单-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(String orderNo, String productNo, String procedureNo,
										 String updateStartTime, String updateEndTime, String scheduleDate,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<SaleOrderMst> queryWrapper = new QueryWrapper<>();
		Page<SaleOrderMst> pageSaleOrderMst = new Page<SaleOrderMst>(page, size);
		if(StringUtil.isNotEmpty(orderNo)) {
			queryWrapper.like("orderNo", orderNo);
		}
		if(StringUtil.isNotEmpty(procedureNo))
		{

		}
		if(StringUtil.isNotEmpty(updateStartTime) && StringUtil.isNotEmpty(updateEndTime))
		{
			queryWrapper.between("dRefDeliveryDate", Timestamp.valueOf(updateStartTime), Timestamp.valueOf(updateEndTime));
		}
		if(StringUtil.isNotEmpty(scheduleDate))
		{

		}
		queryWrapper.orderByDesc("Id");
		IPage<SaleOrderMst> pageList = saleOrderMstService.page(pageSaleOrderMst, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param saleOrderMst
	 * @return
	 */
	@ApiOperation(value="订单-编辑", notes="订单-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody SaleOrderMst saleOrderMst) {
       boolean result = false;
        if(saleOrderMst.getId() == 0L) {
			QueryWrapper<SaleOrderMst> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("orderno", saleOrderMst.getOrderno());

			SaleOrderMst mmMaterialdtl1 = saleOrderMstService.getOne(queryWrapper);
			if (mmMaterialdtl1 != null) {
				String msg = StrUtil.format("编号{}已经存在！", mmMaterialdtl1.getOrderno());
				throw new ApiException(msg);
			}
             result = saleOrderMstService.save(saleOrderMst);
        }
        else
        {
            result = saleOrderMstService.updateById(saleOrderMst);
        }
        return CommonResult.success(result);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="订单-通过id删除", notes="订单-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		saleOrderMstService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="订单-批量删除", notes="订单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("SaleOrderMstIDs") @RequestBody List<Long> ids) {
		this.saleOrderMstService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="订单-通过id查询", notes="订单-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		SaleOrderMst saleOrderMst = saleOrderMstService.getById(id);
		return CommonResult.success(saleOrderMst);
	}
}
